#include "StdAfx.h"
#include "Scale.h"

Scale::Scale(void)
{
}

Scale::~Scale(void)
{
}

void Scale::SetChromatic()
{
	unsigned int ar[] = { /*A2*/110, 117, 123, /*C3*/131, 139, 147, 155, /*E3*/165, 175, 185, 196, 208, /*A3*/220 };
	SetScale(ar, 13);
}

void Scale::SetAMinor()
{
	unsigned int ar[] = { 82, 87, 98, 110, 123, 131, 147, 165, 175, 196, 220, 247, 262, 294, 331 };
	SetScale(ar, 15);
}

void Scale::SetScale(unsigned int ar[], unsigned int length)
{
	scale.clear();
	for(unsigned int i=0; i<length; i++)
	{
		scale.insert(ar[i]);
	}
}

unsigned int Scale::GetNearest(unsigned int frequency)
{	

	std::set<unsigned int>::iterator it = scale.begin();
	unsigned int lower = *it;//._Ptr->_Myval;
	if(frequency <= lower)
		return lower;//to the left end of the available frequencies

	unsigned int higher = lower;
	while(it != scale.end())
	{
		unsigned int cur = *it;
		if(cur > frequency)
		{
			higher = cur;
			break;
		}
		else 
		{
			lower = cur;
			higher = lower;
		}
		it++;
	}

	//if(lower==higher)
	//	return higher;//to the right end of the available frequencies

	int diff1 = frequency-lower;
	int diff2 = higher-frequency;
	if(diff1 <= diff2)
	{
		return lower;
	}
	else
	{
		return higher;
	}
}